package com.nitty.cash.loop.ui.fragment

import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import com.nitty.cash.loop.R
import com.nitty.cash.loop.common.CommonFragment
import com.nitty.cash.loop.databinding.UiPageGuideBinding
import com.nitty.cash.loop.ui.activity.GuideAct

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/11/11 15:03
 * @Annotation：引导页Page
 */
class GuidePageFragment(): CommonFragment() {
    lateinit var binding: UiPageGuideBinding
    private var pageIndex = 0

    constructor(index: Int): this(){
        this.pageIndex = index
    }

    override fun getNittyRootView(inflater: LayoutInflater): View {
        binding = UiPageGuideBinding.inflate(inflater)
        return binding.root
    }

    override fun initNittyView() {
        val backgroundResId: Int
        val lineResId: Int
        val msgStrId: Int
        val nextResId: Int

        val keyColor: Int
        val tipsKey: String
        val showTips: String

        when(pageIndex){
            0 -> {
                backgroundResId = R.mipmap.nitty_guide1_bg
                lineResId = R.mipmap.nitty_guide1_line
                msgStrId = R.string.guide_page1_msg
                nextResId = R.mipmap.nitty_guide1_next

                keyColor = Color.parseColor("#FFFD5118")
                tipsKey = getString(R.string.guide_page1_adjust_credit)
                showTips = String.format(getString(R.string.guide_page1_limit_as_needed),tipsKey)
            }
            1 -> {
                backgroundResId = R.mipmap.nitty_guide2_bg
                lineResId = R.mipmap.nitty_guide2_line
                msgStrId = R.string.guide_page2_msg
                nextResId = R.mipmap.nitty_guide2_next

                keyColor = Color.parseColor("#FF91CDF0")
                tipsKey = getString(R.string.guide_page2_flexible_borrowing)
                showTips = String.format(getString(R.string.guide_page2_repayment),tipsKey)
            }
            else -> {
                backgroundResId = R.mipmap.nitty_guide3_bg
                lineResId = R.mipmap.nitty_guide3_line
                msgStrId = R.string.guide_page3_msg
                nextResId = R.mipmap.nitty_guide3_next

                keyColor = Color.parseColor("#FFA3D100")
                tipsKey = getString(R.string.guide_page3_days)
                showTips = String.format(getString(R.string.guide_page3_term_with_daily_interest),tipsKey)
            }
        }
        binding.guideLayout.setBackgroundResource(backgroundResId)
        binding.imgGuideLine.setImageResource(lineResId)
        binding.textGuideMsg.text = getString(msgStrId)
        binding.imgGuideNext.setImageResource(nextResId)

        val index = showTips.indexOf(tipsKey)
        val spString = SpannableString(showTips)
        spString.setSpan(ForegroundColorSpan(keyColor), index, index + tipsKey.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.textGuideTips.text = spString
    }

    override fun initNittyListener() {
        binding.imgGuideNext.setOnClickListener {
            val guideAct = context as GuideAct
            guideAct.showGuideNextPage(pageIndex+1)
        }
    }

    override fun loadNittyData() {
    }
}